"""
 Retrieves a specific post from Blogger in HTML.
 This service relies on Google GData.
 
 @example /srv/blogger/getpost/blog-id/post-id/
 
 @author Jean-Lou Dupont
 
 My Blog-id : 3285197066205497695
 A post-id:   6216926785348685672
"""
import os
import sys
import logging

import wsgiref.handlers
from google.appengine.ext           import webapp
#from google.appengine.ext.webapp    import template
from libs.wiki import template

from libs.wbblogger import *

class ServiceBlogger( webapp.RequestHandler ):
    """
    Blogger
    """

    def __init__(self):
        self.wbblogger = WBBlogger()

    # SUPPORTED METHODS
    methods = [ 'getpost' ]

    def get(self, method, blog_id, post_id ):
        logging.info("method[%s] blog-id [%s] post-id[%s]" % (method, blog_id, post_id))
        
        [title, content] = self.wbblogger.getPost( blog_id, post_id )
 
        if ( title is not None ) and (content is not None):
            self.printPost( title, content )
        else:
            self.response.set_status( 404 )

    def printPost(self, title, content ):
        
        template_values = { 'title': title , 'content': content };
        
        # Path to the template
        path = os.path.join(os.path.dirname(__file__), 'template.html')
        
        # template replacement & rendering
        self.response.out.write(template.render(path, template_values))
        

#========================================================================================================
def main():
  application = webapp.WSGIApplication([('/srv/blogger/(.*?)/(.*?)/(.*)/', ServiceBlogger)], debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()